package 剑指offer;


/*
 * Author：江松
 * Date：2023/3/19 12:59
 *
 数据流中的中位数：
 [5,2,3,4,1,6,7,0,8]
 当前序列的中位数
"5.00 3.50 3.00 3.50 3.00 3.50 4.00 3.50 4.00 "
1：题解，使用2个堆，[L，mid，R]，左边是大根堆，右边是小根堆，讨论奇偶的情况即可
实现：平衡俩堆数量，差值不能大于1


2:自己想的，用二分查找找到>=num的位置进行插入，维护有序序列求出中位数

***关于二分查找的边界问题：
https://blog.csdn.net/qq_43778308/article/details/108623206
两种场景
找到这个数就返回
找到这个数不返回，继续往左/右找



6 3
1 2 2 3 3 4
3
4
5
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main42 {
    //默认是小顶堆,R
    PriorityQueue<Integer>right=new PriorityQueue<>();
    //x.cmp(y)  x<y L
    PriorityQueue<Integer>left=new PriorityQueue<>((o1,o2)->o2.compareTo(o1));
    public void Insert(Integer num) {
        //加入左边
        left.offer(num);
        //将左边最小值取出放入右边
        right.offer(left.poll());
        //平衡2个堆数量,保证左边大(奇数)
        if(left.size()<right.size()){
            left.offer(right.poll());
        }
    }

    public Double GetMedian() {
        if(left.size()>right.size()){
            return (double)left.peek();
        }else{//偶数
            return (double)(left.peek()+right.peek())/2;
        }
    }
    //二分查找
    public static int left_bound(int a[], int key) {
        int l = 0, r = a.length - 1;
        while (l <= r) {
            int mid = l + ((r - l) >> 1);
            if (a[mid] < key) {
                l = mid + 1;
            } else if (a[mid] > key) {
                r = mid - 1;
            } else  {
                r = mid - 1;//继续向左找
            }
        }
        return l;
    }

    public static int right_bound(int a[], int key) {
        int l = 0, r = a.length - 1;
        while (l <= r) {
            int mid = ((r - l) >> 1)+l ;
            if (a[mid] < key) {
                l = mid + 1;
            } else if (a[mid] > key) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return r;
    }

    static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int n = scanner.nextInt();
        int q = scanner.nextInt();
        int arr[] = new int[n];
        for (int i = 0; i < n; ++i) {
            arr[i] = scanner.nextInt();
        }
        while (q-- > 0) {
            int k = scanner.nextInt();

            int l = left_bound(arr, k);
            //System.out.println(l);
            int r = right_bound(arr, k);
            //System.out.println(r);
            if (l >= n || r < 0 || arr[l] != k || arr[r] != k) {
                System.out.println("-1 -1");
            } else {
                System.out.println(l + " " + r);
            }

        }
    }


    ArrayList<Integer> arr = new ArrayList<>();

    //找到>=key的第一个位置,C++low_bound()
    public int low_bound(int l, int r, int key) {
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (arr.get(mid) < key) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return l;
    }

    public void Insert2(Integer num) {
        int index = low_bound(0, arr.size() - 1, num);
        arr.add(index, num);
    }

    public Double GetMedian2() {
        int len = arr.size();
        if (len % 2 != 0) {
            return (double) arr.get(len / 2);
        } else {
            double avg = ((double) arr.get(len / 2) + (double) arr.get(len / 2 - 1)) / 2;
            return avg;
        }
    }

}
